﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _1._6检测线程状态
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting program...");
            Thread t = new Thread(PrintNumbersWithStatus);
            Thread t2 = new Thread(DoNothing);
            Console.WriteLine("t:" + t.ThreadState.ToString());
            t2.Start();
            t.Start();
            for (int i = 1; i < 30; i++)
            {
                Console.WriteLine("t:" + t.ThreadState.ToString());
            }
            Thread.Sleep(TimeSpan.FromSeconds(6));
            // 线程终止
            t.Abort();
            Console.WriteLine("A thread has been aborted");
            Console.WriteLine("t:" + t.ThreadState.ToString());
            Console.WriteLine("t2:" + t2.ThreadState.ToString());
            Console.ReadLine();
        }

        static void DoNothing()
        {
            Thread.Sleep(TimeSpan.FromSeconds(2));
        }

        static void PrintNumbersWithStatus()
        {
            Console.WriteLine("Starting PrintNumbersWithDelay...");
            // 线程状态
            Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                Console.WriteLine(i);
            }
        }
    }
}
